
package rich.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Test;

/**
 * @author Rich.Li
 * 
 */
public class TestSendMail {
  String host = "";
  String port = "";
  String from = "";
  String to = "";
  String userName = "";
  String passwd = "";
  
  @Test
  public void testcompanySend() {

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    // Get session
    Session session = Session.getInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    try {
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Hello JavaMail");
      message.setText("Welcome to JavaMail");

      Transport trans = session.getTransport("smtp");
      System.out.println("start to connect successfully");
      trans.connect();
      System.out.println("connect successfully");
      // Send message
      Transport.send(message);
    } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

}
